package cn.edu.xmu.oomall.comment.dao.bo;


import cn.edu.xmu.javaee.core.aop.CopyFrom;
import cn.edu.xmu.javaee.core.exception.BusinessException;
import cn.edu.xmu.javaee.core.model.ReturnNo;
import cn.edu.xmu.javaee.core.model.bo.OOMallObject;
import cn.edu.xmu.javaee.core.model.dto.UserDto;
import cn.edu.xmu.oomall.comment.controller.dto.CommentDto;
import cn.edu.xmu.oomall.comment.dao.CommentDao;
import cn.edu.xmu.oomall.comment.mapper.po.CommentPo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.NoArgsConstructor;
import lombok.ToString;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.*;

@NoArgsConstructor
@ToString(callSuper = true, doNotUseGetters = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
@CopyFrom({CommentPo.class, CommentDto.class})
public class Comment extends OOMallObject implements Serializable {
    @ToString.Exclude
    @JsonIgnore
    private final static Logger logger = LoggerFactory.getLogger(Comment.class);

    @ToString.Exclude
    @JsonIgnore
    public static final Byte UNAUDITED = 0;  // 未审核
    @ToString.Exclude
    @JsonIgnore
    public static final Byte APPROVED = 1;    // 已审核
    @ToString.Exclude
    @JsonIgnore
    public static final Byte HIDDEN = 2;      // 已隐藏

    @JsonIgnore
    @ToString.Exclude
    private CommentDao commentDao;

    private Long id;

    private Long shopId;

    private Long productId;

    private Long adminId;

    private Long customerId;

    private Long orderItemId;

    private double score;

    private Byte status;

    private String content;

    private Long pid;


    /**
     * 创建追评
     */
    public Comment addAdditionalComment(Comment additional, UserDto user) {
        List<CommentPo> pos = commentDao.findByPid(this.id);
        if(pos.size() >=2){
            throw new BusinessException(ReturnNo.REPLY_TOO_MUCH, String.format(ReturnNo.REPLY_TOO_MUCH.getMessage(), this.productId, this.id));
        }
        additional.setPid(this.id);
        additional.setOrderItemId(this.orderItemId);
        additional.setProductId(this.productId);
        additional.setStatus(UNAUDITED);
        return this.commentDao.insert(additional, user);
    }

    public Comment createReply(Long shopId, Comment reply, UserDto user) {
        List<CommentPo> pos = commentDao.findByPid(this.id);
        if(pos.size() >=2){
            throw new BusinessException(ReturnNo.REPLY_TOO_MUCH, String.format(ReturnNo.REPLY_TOO_MUCH.getMessage(), this.productId, this.id));
        }
        reply.setPid(this.id);
        reply.setShopId(shopId);
        reply.setStatus(UNAUDITED);
        reply.setProductId(this.productId);
        reply.setOrderItemId(this.orderItemId);
        return this.commentDao.insert(reply, user);
    }

    public void auditComment(UserDto user) {
        this.setStatus(APPROVED);
        this.setAdminId(0L);
        this.commentDao.save(this, user);
    }

    public void hideComment(UserDto user) {
        this.setStatus(HIDDEN);
        this.setAdminId(0L);
        this.commentDao.save(this, user);
    }

    public Long getAdminId() {
        return adminId;
    }

    public void setAdminId(Long adminId) {
        this.adminId = adminId;
    }

    public CommentDao getCommentDao() {
        return commentDao;
    }

    public void setCommentDao(CommentDao commentDao) {
        this.commentDao = commentDao;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Long getCustomerId() {
        return customerId;
    }

    public void setCustomerId(Long customerId) {
        this.customerId = customerId;
    }

    @Override
    public Long getId() {
        return id;
    }

    @Override
    public void setId(Long id) {
        this.id = id;
    }

    public Long getOrderItemId() {
        return orderItemId;
    }

    public void setOrderItemId(Long orderItemId) {
        this.orderItemId = orderItemId;
    }

    public Long getPid() {
        return pid;
    }

    public void setPid(Long pid) {
        this.pid = pid;
    }

    public Long getProductId() {
        return productId;
    }

    public void setProductId(Long productId) {
        this.productId = productId;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    public Long getShopId() {
        return shopId;
    }

    public void setShopId(Long shopId) {
        this.shopId = shopId;
    }

    public Byte getStatus() {
        return status;
    }

    public void setStatus(Byte status) {
        this.status = status;
    }

    public void setGmtCreate(LocalDateTime gmtCreate) {
        this.gmtCreate = gmtCreate;
    }

    public void setGmtModified(LocalDateTime gmtModified) {
        this.gmtModified = gmtModified;
    }

}


